{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c5d19fd4",
   "metadata": {},
   "source": [
    "## Ordinal Encoding"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "83d4a565",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['US'], ['UK'], ['NZ']]\n",
      "[[2.]\n",
      " [1.]\n",
      " [0.]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.preprocessing import OrdinalEncoder\n",
    "\n",
    "\n",
    "data = [[\"US\"], [\"UK\"], [\"NZ\"]]\n",
    "print(data)\n",
    "\n",
    "\n",
    "# our label features\n",
    "encoder = OrdinalEncoder()\n",
    "result = encoder.fit_transform(data)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "60bc735e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2.]\n",
      " [1.]\n",
      " [0.]]\n"
     ]
    }
   ],
   "source": [
    "encoder = OrdinalEncoder()\n",
    "result = encoder.fit_transform(data)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00c87814",
   "metadata": {},
   "source": [
    "## One-Hot Encoding"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "13798a9d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 1.],\n",
       "       [0., 1., 0.],\n",
       "       [1., 0., 0.]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.preprocessing import OneHotEncoder\n",
    "import numpy as np\n",
    "\n",
    "enc = OneHotEncoder(handle_unknown=\"ignore\")\n",
    "data = np.asarray([[\"US\"], [\"UK\"], [\"NZ\"]])\n",
    "enc.fit(data)\n",
    "enc.categories_\n",
    "\n",
    "onehotlabels = enc.transform(data).toarray()\n",
    "onehotlabels"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3675fff7",
   "metadata": {},
   "source": [
    "## Bag of Words\n",
    "[See more here](https://okan.cloud/posts/2021-04-08-text-vectorization-using-python-term-document-matrix/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "92d35261",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.feature_extraction.text import CountVectorizer\n",
    "import pandas as pd\n",
    "\n",
    "# Input data\n",
    "flits = [\n",
    "    \"Hold fast to dreams, for if dreams die, life is a broken-winged bird that cannot fly.\",\n",
    "    \"No bird soars too high if he soars with his own wings.\",\n",
    "    \"A bird does not sing because it has an answer, it sings because it has a song.\",\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "28e41cf3",
   "metadata": {},
   "outputs": [],
   "source": [
    "flits = [\n",
    "    \"Hold fast to dreams, for if dreams die, life is a broken-winged bird that cannot fly.\",\n",
    "    \"No bird soars too high if he soars with his own wings.\",\n",
    "    \"A bird does not sing because it has an answer, it sings because it has a song.\",\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "c7e95e19",
   "metadata": {},
   "outputs": [],
   "source": [
    "vect = CountVectorizer(binary=True)\n",
    "vects = vect.fit_transform(flits)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "2ff5113d",
   "metadata": {},
   "outputs": [],
   "source": [
    "td = pd.DataFrame(vects.todense()).iloc[:5]\n",
    "td.columns = vect.get_feature_names_out()\n",
    "count_vect_df = td\n",
    "term_document_matrix = td.T\n",
    "term_document_matrix.columns = [\"flit_\" + str(i) for i in range(1, 4)]\n",
    "term_document_matrix[\"total_count\"] = term_document_matrix.sum(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "7901c6ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         flit_1  flit_2  flit_3\n",
      "an            0       0       1\n",
      "answer        0       0       1\n",
      "because       0       0       1\n",
      "bird          1       1       1\n",
      "broken        1       0       0\n",
      "cannot        1       0       0\n",
      "die           1       0       0\n",
      "does          0       0       1\n",
      "dreams        1       0       0\n",
      "fast          1       0       0\n"
     ]
    }
   ],
   "source": [
    "print(term_document_matrix.drop(columns=[\"total_count\"]).head(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "6151a0d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>an</th>\n",
       "      <th>answer</th>\n",
       "      <th>because</th>\n",
       "      <th>bird</th>\n",
       "      <th>broken</th>\n",
       "      <th>cannot</th>\n",
       "      <th>die</th>\n",
       "      <th>does</th>\n",
       "      <th>dreams</th>\n",
       "      <th>fast</th>\n",
       "      <th>...</th>\n",
       "      <th>sing</th>\n",
       "      <th>sings</th>\n",
       "      <th>soars</th>\n",
       "      <th>song</th>\n",
       "      <th>that</th>\n",
       "      <th>to</th>\n",
       "      <th>too</th>\n",
       "      <th>winged</th>\n",
       "      <th>wings</th>\n",
       "      <th>with</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3 rows × 34 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   an  answer  because  bird  broken  cannot  die  does  dreams  fast  ...   \n",
       "0   0       0        0     1       1       1    1     0       1     1  ...  \\\n",
       "1   0       0        0     1       0       0    0     0       0     0  ...   \n",
       "2   1       1        1     1       0       0    0     1       0     0  ...   \n",
       "\n",
       "   sing  sings  soars  song  that  to  too  winged  wings  with  \n",
       "0     0      0      0     0     1   1    0       1      0     0  \n",
       "1     0      0      1     0     0   0    1       0      1     1  \n",
       "2     1      1      0     1     0   0    0       0      0     0  \n",
       "\n",
       "[3 rows x 34 columns]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "count_vect_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "bca7ca84",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>an</th>\n",
       "      <th>answer</th>\n",
       "      <th>because</th>\n",
       "      <th>bird</th>\n",
       "      <th>broken</th>\n",
       "      <th>cannot</th>\n",
       "      <th>die</th>\n",
       "      <th>does</th>\n",
       "      <th>dreams</th>\n",
       "      <th>...</th>\n",
       "      <th>sing</th>\n",
       "      <th>sings</th>\n",
       "      <th>soars</th>\n",
       "      <th>song</th>\n",
       "      <th>that</th>\n",
       "      <th>to</th>\n",
       "      <th>too</th>\n",
       "      <th>winged</th>\n",
       "      <th>wings</th>\n",
       "      <th>with</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Hold fast to dreams, for if dreams die, life i...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>No bird soars too high if he soars with his ow...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A bird does not sing because it has an answer,...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3 rows × 35 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                   0  an  answer  because   \n",
       "0  Hold fast to dreams, for if dreams die, life i...   0       0        0  \\\n",
       "1  No bird soars too high if he soars with his ow...   0       0        0   \n",
       "2  A bird does not sing because it has an answer,...   1       1        1   \n",
       "\n",
       "   bird  broken  cannot  die  does  dreams  ...  sing  sings  soars  song   \n",
       "0     1       1       1    1     0       1  ...     0      0      0     0  \\\n",
       "1     1       0       0    0     0       0  ...     0      0      1     0   \n",
       "2     1       0       0    0     1       0  ...     1      1      0     1   \n",
       "\n",
       "   that  to  too  winged  wings  with  \n",
       "0     1   1    0       1      0     0  \n",
       "1     0   0    1       0      1     1  \n",
       "2     0   0    0       0      0     0  \n",
       "\n",
       "[3 rows x 35 columns]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([doc, count_vect_df], axis=1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "embed",
   "language": "python",
   "name": "embed"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
